package leeon.bbsbrower.views;

import leeon.bbsbrower.actions.BBSLogAction;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

public class MainView extends ViewPart {
	public static final String ID = "bbsbrower.view.main";

	private List list = null;

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		
		new Label(parent, SWT.NONE);
		
		//主菜单
		list = new List(parent, SWT.NONE);
		list.setFont(ApplicationWorkbenchWindowAdvisor.defaultFont);
		
		//写死
		list.add("分类讨论区");
		list.add("我的收藏夹");
		list.add("上鹊桥");
		list.add("处理信笺");
		list.add("所有讨论区");
		list.add("说拜拜");
		
		//登陆进来时选中0
		list.select(0);
		
		//上下左右的时间
		list.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//左
				if (e.keyCode == SWT.ARROW_LEFT) {
					list.select(5);
					e.doit = false;
				//右
				} else if (e.keyCode == SWT.ARROW_RIGHT || e.keyCode == 13) {										
					//分类讨论区
					if (list.getSelectionIndex() == 0) {
						actionBlocks();
					//收藏夹	
					} else if (list.getSelectionIndex() == 1) {
						actionMyBoards();						
					//上鹊桥
					} else if (list.getSelectionIndex() == 2) {
						actionFriends();						
					//处理信笺
					} else if (list.getSelectionIndex() == 3) {
						actionMails();
					//所有讨论区
					} else if (list.getSelectionIndex() == 4) {
						actionAllBoards();
					//说拜拜
					} else {
						actionLogout();
					}															
					e.doit = false;
				} 
			}

			public void keyReleased(KeyEvent e) {}
			
		});
		
		new Label(parent, SWT.NONE);
	}
	
	
	/**
	 * logout的action
	 */
	private void actionLogout() {
		//退出的action
		try {
			BBSLogAction.logout();
		} catch (Exception e) {
			MessageDialog.openError(this.getViewSite().getShell(), "错误信息", e.toString());
			e.printStackTrace();
			return;
		}
		
		//跳转到登陆界面
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			page.showView(LoginView.ID);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 到各讨论块
	 */
	private void actionBlocks() {
		//跳转到讨论区界面
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			BlockView block = (BlockView)page.showView(BlockView.ID);
			block.refreshData(0);
			
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 到收藏夹
	 */
	private void actionMyBoards() {
		//跳转到我的收藏界面
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			MyBoardView board = (MyBoardView)page.showView(MyBoardView.ID);
			board.refreshData(0);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 到鹊桥
	 */
	private void actionFriends() {		
		//跳转到鹊桥界面
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			page.showView(FriendView.ID);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 到信件处理
	 */
	private void actionMails() {
		if ("guest".equals(BBSLogAction.id)) {
			MessageDialog.openWarning(this.getViewSite().getShell(), "主菜单", "请先登陆");;
			return;
		}
		
		//跳转到新建处理界面
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			page.showView(MailView.ID);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 到所有讨论区
	 */
	private void actionAllBoards() {
		if ("guest".equals(BBSLogAction.id)) {
			MessageDialog.openWarning(this.getViewSite().getShell(), "主菜单", "请先登陆");;
			return;
		}
		
		//跳转到所有讨论区界面
		IWorkbenchPage page = this.getViewSite().getPage();
		page.hideView(this);
		try {
			AllBoardView board = (AllBoardView)page.showView(AllBoardView.ID);
			board.refreshData(0);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 刷新数据
	 *
	 */
	public void refreshData(int index) {
		ApplicationActionBarAdvisor.setStatusLine();
		list.select(index);
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		list.setFocus();
		ApplicationActionBarAdvisor.setStatusLine();
	}
}